package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.web.util.CookieUtil;
import cn.wolfcoe.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {
    @Reference
    private IUserInfoService userInfoService;

    @RequestMapping("/user")
    @ResponseBody
    public Object get() {
        UserInfo userInfo = userInfoService.get( 4L );
        return userInfo;
    }

    //返回值如果是false,页面取出来的是 是js对象,也是
    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone) {
        return !userInfoService.checkPhone( phone );
    }

    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode( phone );
        return new AjaxResult( true, "验证码发送成功" );
    }

    //注册,要返回注册或者失败
    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        userInfoService.userRegist( phone, nickname, password, rpassword, verifyCode );
        return new AjaxResult( true, "注册成功" );
    }

    //用户登录
    @RequestMapping("/userLogin")
    @ResponseBody
    public AjaxResult  userLogin(String username, String password, HttpServletResponse response) {
     String token=  userInfoService.userLogin(username,password );
        CookieUtil.addCookie( response,token );
        return new AjaxResult( true, "登录成功" );
    }



}
